const { PrismaClient } = require('@prisma/client');
const bcryptjs = require('bcryptjs');
const prisma = new PrismaClient();

async function testLogin() {
  try {
    const identifier = 'admin';
    const password = 'password123';
    
    // 找到用户
    const user = await prisma.user.findFirst({
      where: {
        OR: [
          { email: identifier },
          { username: identifier }
        ]
      },
      include: {
        secrets: true
      }
    });
    
    if (!user) {
      console.error('用户不存在');
      return;
    }
    
    console.log('找到用户:', user.username);
    
    // 验证密码
    if (!user.secrets?.password) {
      console.error('用户密码未设置');
      return;
    }
    
    const isPasswordValid = await bcryptjs.compare(password, user.secrets.password);
    console.log('密码验证:', isPasswordValid ? '成功' : '失败');
    
    if (isPasswordValid) {
      // 尝试更新最后登录时间，不更新refreshToken
      try {
        await prisma.secrets.update({
          where: { userId: user.id },
          data: { 
            lastSignedIn: new Date(),
            // 暂时不更新refreshToken
          }
        });
        console.log('更新最后登录时间成功');
      } catch (updateError) {
        console.error('更新最后登录时间失败:', updateError);
      }
    }
  } catch (error) {
    console.error('登录测试失败:', error);
  } finally {
    await prisma.$disconnect();
  }
}

testLogin(); 